const { envList } = require('../../envList');

// pages/me/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    islogin: false,
    showTip: false,
    title:"",
    content:"",
    _nickName: "",
    _phone: "授权手机号",
    _avatarUrl: "../../images/icons/avatar.png"
  },
  onShow(){
    const islogin = wx.getStorageSync('islogin')
    if(islogin){
      const userInfo = wx.getStorageSync('userInfo')
      this.setData({
        islogin,
        ...userInfo
      })
    }
  },
  onInputChange: function (e) {
    console.log(e);
    this.setData({
      _nickName: e.detail.value
    });
  },
  onChooseAvatar(e) {
    console.log(e);
    const { avatarUrl } = e.detail
    this.setData({
      _avatarUrl: avatarUrl,
    })
  },
  login(){
    wx.showLoading({
      title: '',
    });
    wx.cloud
    .callFunction({
      name: 'quickstartFunctions',
      data: {
        "type": 'getOpenId',
        "action": "login"
      },
    })
    .then((resp) => {
      if(resp.result.code == 200){
        wx.showToast({
          title: '登录成功！',
        })
      const userInfo = resp.result.data
      wx.setStorageSync('islogin', true)
      wx.setStorageSync('userInfo', userInfo)
      this.setData({
        islogin: true
      });
      this.onShow()
      }else{
        wx.showToast({
          title: resp.result.msg,
          icon: 'error'
        })
      }
      wx.hideLoading();
    })
    .catch((e) => {
      wx.hideLoading();
    });
  },
  logout(){
    wx.clearStorageSync();
    this.setData({
      islogin: false,
      _nickName: "",
    _phone: "授权手机号",
    _avatarUrl: "../../images/icons/avatar.png"
    })
  },
  

  async getPhoneNumber(event) {
    const errMsg = event.detail.errMsg
    const _id = this.data._id
    console.log(event)
    // 判断用户是否点击允许
    if (errMsg === "getPhoneNumber:ok") {
      const cloudId = event.detail.cloudID
      const cloudIdList = [cloudId]
      // 请求云函数
      const cloudFunRes = await wx.cloud.callFunction({
        name: "quickstartFunctions", // 云函数名
        data: { 
          type: "getPhone",
          _id,
          cloudIdList
        }
      })
      if(cloudFunRes.result.code == 200){
        let phone = cloudFunRes.result.data
        console.log(phone);
        this.setData({
          _phone: phone
        })
      }
      else{
        wx.showToast({
          title: cloudFunRes.result.msg,
          icon: "error"
        })
      }
    }
  },
  async getOpenId() {
    wx.showLoading({
      title: '',
    });
    const {_phone, _avatarUrl, _nickName} = this.data
    console.log(_phone);
    console.log(_avatarUrl);
    console.log(_nickName);
    const {fileID} = await wx.cloud.uploadFile({
      cloudPath: 'avatar/' + Math.floor(Math.random()*1000000),
      filePath: _avatarUrl,
    })
    wx.cloud
      .callFunction({
        name: 'quickstartFunctions',
        data: {
          "type": 'getOpenId',
          _phone,
          "_avatarUrl": fileID,
          _nickName
        },
      })
      .then((resp) => {
        console.log(resp.result);
        if(resp.result.code == 200){
          wx.showToast({
            title: '授权成功！',
          })
        const userInfo = resp.result.data
        wx.setStorageSync('islogin', true)
        wx.setStorageSync('userInfo', userInfo)
        this.setData({
          islogin: true
        });
        }else{
          this.setData({
            _nickName: "",
            _phone: "授权手机号",
            _avatarUrl: "../../images/icons/avatar.png"
          });
          wx.showToast({
            title: resp.result.msg,
            icon: 'error'
          })
        }
        wx.hideLoading();
      })
      .catch((e) => {
        wx.hideLoading();
      });
  },

  gotoWxCodePage() {
    wx.navigateTo({
      url: `/pages/exampleDetail/index?envId=${envList?.[0]?.envId}&type=getMiniProgramCode`,
    });
  },
});
